using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Portal.User.Security;
using Portal.Core.DAL;
using Portal.BO.Editoral.LogNews;
using Portal.BO.Editoral.Newslist;
using Portal.BO.Editoral.Menu;
using System.Xml;
using System.Text;

namespace Portal.GUI.EditoralOffice.MainOffce.Menu
{
	public partial class menu : System.Web.UI.UserControl
	{
		
		public string CurrentMenuItem = string.Empty;
		XmlDocument doc = null;

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				getCurrentMenuItem();
				string userName = HttpContext.Current.User.Identity.Name;
				MainSecurity mainSecurity = new MainSecurity();
				DataTable tblPermissions = mainSecurity.GetPermissionAsTable(userName);
                Role objRole = mainSecurity.GetRole(Page.User.Identity.Name);
				if (tblPermissions != null)
				{
					
					doc = MenuCommon.getXML();
					XmlNodeList boxes = doc.SelectNodes("boxes/box[@isvisible='true']");
					StringBuilder html = new StringBuilder();
					bool isCheckPermisson = false, isCount = false;
					XmlNodeList rows = null;
					string a = string.Empty;
					foreach (XmlNode box in boxes)
					{
						html.AppendLine("<li class=\"box\">");
						html.AppendLine("<div class=\"box\">");
						html.AppendLine(box.SelectSingleNode("name").InnerText);
						html.AppendLine("</div>");

						isCheckPermisson = bool.Parse(box.SelectSingleNode("@checkpermission").InnerText);

						html.AppendLine("<ul>");
						rows = box.SelectNodes("rows/row");
						foreach (XmlNode row in rows)
						{
							if (isCheckPermisson && tblPermissions.Select("Permission_ID=" + row.SelectSingleNode("Permission_ID").InnerText).Length == 1 || !isCheckPermisson)
							{
								html.AppendLine("<li class=\"menumodule\">");
								isCount = bool.Parse(row.SelectSingleNode("IsCount").InnerText);
								a = "<a onclick=\"menu_logHref('{0}'); return true;\" href=\"/office/{0}.chn\">+ {1} {2}</a>";
								if (isCount)
									a = string.Format(a, row.SelectSingleNode("Cpmode").InnerText, row.SelectSingleNode("MenuName").InnerText, "(<span>" + MenuCommon.CafeF_GetNewsCountStr(row.SelectSingleNode("Cpmode").InnerText, objRole) + "</span>)");
								else
									a = string.Format(a, row.SelectSingleNode("Cpmode").InnerText, row.SelectSingleNode("MenuName").InnerText,string.Empty);
								
								html.AppendLine(a);
								html.AppendLine("</li>");
							}
						}
						html.AppendLine("</ul>");

						
						html.AppendLine("</li>");
					}
					ltrHtml.Text = html.ToString();
				}

                Role objrole = mainSecurity.GetRole(Page.User.Identity.Name);
				userName = userName.ToLower();
                if (userName == "admin" || userName == "channelvn" || objrole.isPhuTrachKenh || objrole.isTongBienTap || objrole.isQuanTriKenh)
					div2.Visible = true;
				else
					div2.Visible = false;
			}
		}

		private void getCurrentMenuItem()
		{
			string strMode = Request.QueryString["cpmode"];
			if (strMode.IndexOf(",") > 0) strMode = strMode.Substring(0, strMode.IndexOf(","));

			XmlDocument doc = MenuCommon.getXML();
			XmlAttribute ownerCpMode = (XmlAttribute)doc.SelectSingleNode("//row[Cpmode = '" + strMode + "']/@ownerCpMode");
			if (ownerCpMode != null) CurrentMenuItem = ownerCpMode.InnerText;
		}

		protected void itemLogOut_Click(object sender, EventArgs e)
		{
			HttpCookie cookie = Request.Cookies["PortalUser"];
			if (cookie != null)
			{
				cookie.Values["AC"] = "";
				cookie.Values["PW"] = "";
				DateTime dt = DateTime.Now;
				dt.AddDays(-1);
				cookie.Expires = dt;
				Response.Cookies.Add(cookie);

			}
			if (Request.Cookies["pu"] != null) Response.Cookies.Remove("pu");
			LogRow logRow = new LogRow();
			logRow.News_ID = 0;
			logRow.Type = (int)LogType.Login;
			logRow.Action = Log.LOGOUT;
			logRow.CreatedDate = DateTime.Now;
			logRow.UserName = HttpContext.Current.User.Identity.Name;
			LogHelper logHelper = new LogHelper();
			logHelper.Insert(logRow);

			FormsAuthentication.SignOut();
			Context.User = null;
			Response.Redirect("/Login.chn");
		}

	}
}